/*
 * thread.h
 *
 *  Created on: 03/10/2011
 *      Author: guido
 */

#ifndef THREAD_H_
#define THREAD_H_

#include <pthread.h>
#include "runnable.h"

class Thread {
	public:
		Thread(Runnable* metodo) {
			this->metodo = metodo;
			this->alive = true;
		}
		void start() {
			pthread_create(&thread, NULL, Thread::startThread, (void*) this);
		}
		~Thread() {
			pthread_join(thread, NULL);
			delete metodo;
		};
		bool isAlive() {
			return alive;
		}
		void signalDead() {
			alive = false;
		}
	private:
		static void* startThread(void* arg) {
			Thread* thread = static_cast<Thread*>(arg);
			thread->metodo->run();
			thread->signalDead();
		}
		Runnable* metodo;
		pthread_t thread;
		bool alive;
};


#endif /* THREAD_H_ */
